package com.tan.firstblog.controllers;

import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.tan.firstblog.models.Post;
import com.tan.firstblog.service.interfaces.PostService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	private PostService postService;
	
	@Autowired(required=true)
	@Qualifier(value="postService")
	public void setPostService(PostService postService) {
		this.postService = postService;
	}

	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		List<Post> listPost = this.postService.getAllPosts();
	
		ModelAndView mav = new ModelAndView("home", "listPost", listPost);
		
		return mav;
	}

	@RequestMapping(value = "/abc", method = RequestMethod.GET)
	public ModelAndView test(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		List<Post> listPost = this.postService.getAllPosts();
	
		ModelAndView mav = new ModelAndView("home", "listPost", listPost);
		
		return mav;
	}
	
}
